package com.framework.loippi.controller.api;

import com.framework.loippi.api.utils.ApiUtils;
import com.framework.loippi.entity.Address;
import com.framework.loippi.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AddressController
 * @Description TODO
 * @Author fsociety
 * @Date 29/12/2019 上午10:26
 * @Version 1.0
 */

@Controller("apiAddressController")
@RequestMapping("/api/address")
@Api(tags = {"用户地址"})
public class AddressController extends ApiBaseController {

    @Resource
    private AddressService addressService;

   @RequestMapping(value = "/add.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
   @ApiOperation("添加地址")
   public @ResponseBody
   String addAddress(Address address) {
      addressService.save(address);

      return ApiUtils.success();
   }

    @RequestMapping(value = "findAll.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "查询地址", notes = "{}")
    public @ResponseBody
    String findAll(Integer userId) {
        List<Address> list = addressService.findByUserId(userId);
        Map<String,Object> map = new HashMap<>();
        for (Address ad : list) {
            if (ad.getStatus() != null && ad.getStatus() == 1) {
                map.put("default", ad);
            }
            map.put("all", list);
        }
        return ApiUtils.success(map);
    }

}
